package com.zyh.thread;

/**
 * @Author zyh
 * @Date 2024/8/7 16:05
 * @Description 线程让步 yield
 */

public class ThreadDemo6 {
    public static void main(String[] args) {
        MyTask03 myTask03 = new MyTask03();
        Thread t1 = new Thread(myTask03, "线程A");
        t1.start();
        MyThread03 t2 = new MyThread03("---线程B");
        t2.start();
    }
}

class MyThread03 extends Thread{
    public MyThread03() {
    }

    public MyThread03(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"正在打印"+i);
        }
    }
}

class MyTask03 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Thread.yield();
            System.out.println(Thread.currentThread().getName()+"正在打印"+i);
        }
    }
}